﻿using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace HostsUpdaterCore
{
    public class Common
    {
        /// <summary>
        /// 日志记录辅助方法
        /// </summary>
        /// <param name="info">待写入的信息</param>
        /// <param name="logPath">日志路径</param>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static void Log(string info, string logPath)
        {
            if (!string.IsNullOrEmpty(info))
            {
                string directoryName = Path.GetDirectoryName(logPath);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                logPath += "/log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                using (StreamWriter writer = new StreamWriter(logPath, true))
                {
                    writer.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.ff") + "] " + info);
                }
            }
        }
    }
}
